"use strict";

let numInputElement;
let promptElement;
let targetNum;
let tryTimes = 0;
let minInputValue;
let maxInputValue;
let valueInput;

document.addEventListener("DOMContentLoaded", function(event) {
    numInputElement = document.getElementById("numInput");
    promptElement = document.getElementById('prompt');
    minInputValue = document.getElementById('min');
    maxInputValue = document.getElementById('max');
    valueInput = document.getElementById('valueInput');
    targetNum = getRandom(min, max);
    console.log(targetNum);
});

function getRandom(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min
}

function onTry() {
    let minValue = minInputValue.value;
    let maxValue = maxInputValue.value;
    if (minValue === ''||maxValue === '') {
        return
    }
    if (isNaN(minValue) || isNaN(maxValue)) {
        valueInput.innerHTML = '输入无效，请重新输入';
    } else {
        if (minValue < 0) {
            valueInput.innerHTML = '最小值应该大于等于 0';
        } else if (maxValue < minValue) {
            valueInput.innerHTML = '最大值应该大于最小值';
        }
    }
    promptElement.style.display = 'block';
}

function onSubmit() {
    let numValue = numInputElement.value;
    if (numValue === '') {
        return
    }
    tryTimes++;
    if (isNaN(numValue)) {
        promptElement.innerHTML = `你的输入不是一个数字：${numValue}, 你已经尝试了${tryTimes}次`;
    } else {
        if (numValue > targetNum) {
            promptElement.innerHTML = `太大了, 你已经尝试了${tryTimes}次`;
        } else if (numValue < targetNum) {
            promptElement.innerHTML = `太小了, 你已经尝试了${tryTimes}次`;
        } else {
            promptElement.innerHTML = `恭喜，猜对了！, 你已经尝试了${tryTimes}次`;
            promptElement.classList.remove('alert-danger');
            promptElement.classList.add('alert-success');
        }
    }
    
    promptElement.style.display = 'block';
}